<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page
	import="java.util.List,java.util.Map,java.util.HashMap,hyl.core.MyFun,com.servlet.*"%>
<%@ page import="hyl.ext.base.*,com.dao.SqlOfUser,hyl.core.MyFun"%>
<%@ page import="hyl.ext.web.ms.*,com.alibaba.fastjson.JSON"%>
<%@ include file="pub/top.jsp"%>
<%
//b 系统 不在线 退出
// 如果 appidb 不在审批通过名单 不允许访问 
// 如果AB系统没有账号关联 不能访问
// 用户有b系统 有效会话 则直接登录
//  重新获取新的sessionb
//用 sessionb 登录

TMsA _ma = My.getMsA();
String appidB = request.getParameter("app");
boolean b在线 = _ma.isBappOnline(appidB);
if (!b在线) {
	request.setAttribute("code", 1);
	request.setAttribute("msg", "该服务无不在线");
	request.setAttribute("url", "index.html");
	request.getRequestDispatcher("err.jsp").forward(request, response);
	return;
}
BInfo bInfo = _ma.getBInfo(appidB);
if (bInfo.getIstate() < 2) {
	return;
}

MySession sessiona = SessionFactory.getSession(request, response);
int usera = MyFun.str2int(request.getParameter("usera"));
int userb = MyFun.str2int(request.getParameter("userb"));

if (sessiona.getUserId() != usera) {
	request.setAttribute("code", 3);
	request.setAttribute("msg", "此用户无权访问应用系统");
	request.getRequestDispatcher("err.jsp").forward(request, response);
	return;
}

ABOpen opena = _ma.getABOpen1(usera, appidB);
if (opena == null || MyFun.isEmpty(opena.getSUidB())) {
	request.setAttribute("code", 3);
	request.setAttribute("msg", "该系统账号未关联");
	request.setAttribute("url", "regBUser.jsp");
	request.getRequestDispatcher("err.jsp").forward(request, response);
	return;
}

String bUrl = bInfo.get("BURL");
String sessip = sessiona.getIp();
String stampid = MyFun.getNsDesc();
//MyFun.print(appidB, TMsA.S获取会话, opena.getSUidB(), opena.sidB, sessip);
BInfo bi = _ma.getBInfo(appidB);

String mw = bi.aes加密64(MyFun.join2(",", stampid, opena.getSUidB(), sessip));
String sessionb = _ma.req向B提问(appidB, TMsA.S获取会话, stampid, mw);
if (MyFun.isBlank(sessionb)) {
	//没有session ,也没有useridb 和ip 时
	request.setAttribute("code", 0);
	request.setAttribute("msg", userb + "该账号未登录");
	request.getRequestDispatcher("err.jsp").forward(request, response);
	return;
}

opena.sidA = sessiona.getTokenId();
opena.sidB = sessionb;

_ma.updateABOpen(opena);

String tokenb = _ma.req向B提问(appidB, TMsA.S获取令牌, stampid);
//MyFun.print("11", tokenb, sessionb);
String 明文 = MyFun.join2(",", appidB, sessionb, tokenb);

mw = bi.aes加密64(明文);
// 有sessionid_b 通过前端直接访问,也会因为ip 不一样 导致无法登录
// 如果sessionid_b 为空或不正确 让b创建一个新的 sessionid_b
String url = MyFun.join(bUrl, "/ssob/a_b?mw=", mw);
//MyFun.print(url);
response.sendRedirect(url);
%>